<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片翻转工具 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .upload-area {
            border: 2px dashed rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.05);
        }

        .upload-area:hover {
            border-color: #00ff88;
            background: rgba(255, 255, 255, 0.1);
        }

        .workspace {
            display: flex;
            gap: 20px;
            margin: 30px 0;
            flex-wrap: wrap;
        }

        .canvas-container {
            flex: 1;
            min-width: 300px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            overflow: hidden;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .controls {
            width: 250px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            padding: 20px;
        }

        .control-group {
            margin-bottom: 20px;
        }

        .control-group h3 {
            margin-bottom: 10px;
            color: rgba(255, 255, 255, 0.9);
        }

        .flip-btn {
            width: 100%;
            padding: 12px;
            margin: 5px 0;
            border: none;
            border-radius: 6px;
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .flip-btn:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .flip-btn.active {
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
        }

        #canvas {
            max-width: 100%;
            height: auto;
        }

        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 30px;
        }

        .action-btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 255, 136, 0.3);
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        @media (max-width: 768px) {
            .workspace {
                flex-direction: column;
            }

            .controls {
                width: 100%;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>图片翻转工具</h1>
            <p>水平或垂直翻转图片</p>
        </div>

        <div class="upload-area" id="uploadArea">
            <div class="upload-text">点击或拖拽图片到这里</div>
            <input type="file" id="imageInput" accept="image/*" style="display: none;">
        </div>

        <div class="workspace">
            <div class="canvas-container">
                <canvas id="canvas"></canvas>
            </div>
            <div class="controls">
                <div class="control-group">
                    <h3>翻转方向</h3>
                    <button class="flip-btn" data-flip="horizontal">
                        <span>⇄</span>
                        <span>水平翻转</span>
                    </button>
                    <button class="flip-btn" data-flip="vertical">
                        <span>⇅</span>
                        <span>垂直翻转</span>
                    </button>
                    <button class="flip-btn" data-flip="both">
                        <span>⤡</span>
                        <span>水平+垂直翻转</span>
                    </button>
                </div>
            </div>
        </div>

        <div class="action-buttons">
            <button class="action-btn" id="resetBtn">重置</button>
            <button class="action-btn" id="downloadBtn">下载</button>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const uploadArea = document.getElementById('uploadArea');
        const imageInput = document.getElementById('imageInput');
        let originalImage = null;
        let isFlippedHorizontal = false;
        let isFlippedVertical = false;

        // 文件处理
        uploadArea.addEventListener('click', () => imageInput.click());
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.style.borderColor = '#00ff88';
        });
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.style.borderColor = 'rgba(255, 255, 255, 0.2)';
        });
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                handleImage(file);
            }
        });

        imageInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                handleImage(file);
            }
        });

        function handleImage(file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage = new Image();
                originalImage.onload = () => {
                    resetCanvas();
                };
                originalImage.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }

        function resetCanvas() {
            isFlippedHorizontal = false;
            isFlippedVertical = false;
            document.querySelectorAll('.flip-btn').forEach(btn => btn.classList.remove('active'));
            
            canvas.width = originalImage.width;
            canvas.height = originalImage.height;
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.drawImage(originalImage, 0, 0);
        }

        function flipImage(type) {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const btn = document.querySelector(`[data-flip="${type}"]`);
            
            switch(type) {
                case 'horizontal':
                    isFlippedHorizontal = !isFlippedHorizontal;
                    btn.classList.toggle('active');
                    break;
                case 'vertical':
                    isFlippedVertical = !isFlippedVertical;
                    btn.classList.toggle('active');
                    break;
                case 'both':
                    isFlippedHorizontal = !isFlippedHorizontal;
                    isFlippedVertical = !isFlippedVertical;
                    document.querySelector('[data-flip="horizontal"]').classList.toggle('active');
                    document.querySelector('[data-flip="vertical"]').classList.toggle('active');
                    btn.classList.toggle('active');
                    break;
            }

            // 重新绘制图片
            ctx.save();
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 设置翻转变换
            ctx.translate(canvas.width / 2, canvas.height / 2);
            ctx.scale(isFlippedHorizontal ? -1 : 1, isFlippedVertical ? -1 : 1);
            ctx.translate(-canvas.width / 2, -canvas.height / 2);
            
            // 绘制图片
            ctx.drawImage(originalImage, 0, 0);
            ctx.restore();
        }

        // 翻转按钮事件
        document.querySelectorAll('.flip-btn').forEach(btn => {
            btn.addEventListener('click', () => {
                flipImage(btn.dataset.flip);
            });
        });

        // 重置按钮
        document.getElementById('resetBtn').addEventListener('click', () => {
            if (originalImage) {
                resetCanvas();
            }
        });

        // 下载按钮
        document.getElementById('downloadBtn').addEventListener('click', () => {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const link = document.createElement('a');
            link.download = 'flipped_image.png';
            link.href = canvas.toDataURL('image/png');
            link.click();
            toast.success('开始下载');
        });
    </script>
</body>
</html> 